//
//  CompositeViewController.m
//  MansApp
//
//  Created by lanou3g on 15/7/13.
//  Copyright (c) 2015年 ACH. All rights reserved.
//

#import "CompositeViewController.h"

#import "ScienceTechnologyViewController.h"//科学频道
#import "CompositCollectionViewCell.h"
#import "CompositModel.h"
#import "HeadCollectionReusableView.h"
#import "MovieDetailViewController.h"
#import "CarBossViewController.h"
#import "UIViewController+MMDrawerController.h"
@interface CompositeViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,SDCycleScrollViewDelegate,UIAlertViewDelegate>
@property (nonatomic,strong) UICollectionView *collectionView;
@property (nonatomic,strong) NSArray *compositeArray;
@property (nonatomic,strong) NSMutableArray *scrollArray;
@property (nonatomic,strong) NSMutableArray *titleArray;
@property (nonatomic,strong) NSMutableArray *imageArray;
@property (nonatomic,strong) NSMutableArray *idArray;//装载轮播图的ID
@property(nonatomic,strong)NSArray *iconArray;

@property (nonatomic,strong) UIImageView *weatherImage;
@property (nonatomic,strong) UIView *backView;
@property (nonatomic,strong) UILabel *detailLabel;
@property (nonatomic,strong) UILabel *addressLable;
@property (nonatomic,strong) UIImageView *dingweiPic;
@end

@implementation CompositeViewController


- (void)viewDidLoad {
    [super viewDidLoad];
        
    self.backView = [[UIView alloc]initWithFrame:CGRectMake(kP6Height(0), kP6Height(0), kP6Height(90), kP6Height(44))];
    self.backView.backgroundColor = [UIColor clearColor];
    
    self.weatherImage = [[UIImageView alloc]initWithFrame:CGRectMake(kP6Height(0), kP6Height(0), kP6Height(43), kP6Height(30))];
    [self.backView addSubview:_weatherImage];
    
    self.detailLabel = [[UILabel alloc]initWithFrame:CGRectMake(kP6Height(0), kP6Height(30), kP6Height(43), kP6Height(10))];
    self.detailLabel.textAlignment = NSTextAlignmentCenter;
    self.detailLabel.font = [UIFont systemFontOfSize:10];
    [self.backView addSubview:_detailLabel];
    
    self.addressLable = [[UILabel alloc]initWithFrame:CGRectMake(kP6Height(43), kP6Height(10), kP6Height(50), kP6Height(30))];
    self.addressLable.font = [UIFont systemFontOfSize:14];
    [self.backView addSubview:_addressLable];

    UIBarButtonItem *rigthButton = [[UIBarButtonItem alloc]initWithCustomView:_backView];
    self.navigationItem.rightBarButtonItem = rigthButton;
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"article_special_title_bg"] forBarMetrics:UIBarMetricsDefault];
    [self judgeForNetWork];
}
// 判断是否有网络
- (void)judgeForNetWork
{
    if (![[NetWork shareNetWork]rechability]) {
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"网络连接失败" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
        return;
    }
    else
    {
        [self openThread];
    }
}


- (void)openThread
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSLog(@"所在线程<---->%d",[NSThread isMainThread]);
        // 回到主线程
        dispatch_async(dispatch_get_main_queue(), ^{
            [self loadWeather];
            NSLog(@"所在线程---->>>%d",[NSThread isMainThread]);
        });
    });
}

#pragma mark ==== 定位和天气 ====
- (void)loadWeather
{
    [[MapAndLocation shareLocation]starLocationNow:^(BOOL finsh, NSString *city, id data) {
        NSString *sweather = [[NSString alloc]initWithFormat:@"%@",[MapAndLocation shareLocation].temp1];
        
        self.addressLable.text = [MapAndLocation shareLocation].locationAddress;
        NSString *fileName=@"晴.png";
        if(NSNotFound != [[MapAndLocation shareLocation].weather rangeOfString:@"晴"].location)
        {
            fileName =[[NSString alloc]initWithFormat:@"%@",@"晴.png"];
        }
        if(NSNotFound != [[MapAndLocation shareLocation].weather rangeOfString:@"多云"].location)
        {
            fileName =[[NSString alloc]initWithFormat:@"%@", @"多云.png"];
        }
        if(NSNotFound != [[MapAndLocation shareLocation].weather rangeOfString:@"阴"].location)
        {
            fileName =[[NSString alloc]initWithFormat:@"%@", @"阴.png"];
        }
        if(NSNotFound != [[MapAndLocation shareLocation].weather rangeOfString:@"雨"].location)
        {
            fileName =[[NSString alloc]initWithFormat:@"%@", @"中雨.png"];
        }
        if(NSNotFound != [[MapAndLocation shareLocation].weather rangeOfString:@"雪"].location)
        {
            fileName =[[NSString alloc]initWithFormat:@"%@", @"小雪.png"];
        }
        if(NSNotFound != [[MapAndLocation shareLocation].weather rangeOfString:@"雷"].location)
        {
            fileName =[[NSString alloc]initWithFormat:@"%@", @"雷雨.png"];
        }
        if(NSNotFound != [[MapAndLocation shareLocation].weather rangeOfString:@"雾"].location)
        {
            fileName =[[NSString alloc]initWithFormat:@"%@", @"雾.png"];
        }
        if(NSNotFound != [[MapAndLocation shareLocation].weather rangeOfString:@"大雪"].location)
        {
            fileName =[[NSString alloc]initWithFormat:@"%@", @"大雪.png"];
        }
        if(NSNotFound != [[MapAndLocation shareLocation].weather rangeOfString:@"大雨"].location)
        {
            fileName =[[NSString alloc]initWithFormat:@"%@", @"大雨.png"];
        }
        if(NSNotFound != [[MapAndLocation shareLocation].weather rangeOfString:@"中雪"].location)
        {
            fileName = [[NSString alloc]initWithFormat:@"%@",@"中雪.png"];
        }
        if(NSNotFound != [[MapAndLocation shareLocation].weather rangeOfString:@"中雨"].location)
        {
            fileName = [[NSString alloc]initWithFormat:@"%@",@"中雨.png"];
        }
        if(NSNotFound != [[MapAndLocation shareLocation].weather rangeOfString:@"小雪"].location)
        {
            fileName = [[NSString alloc]initWithFormat:@"%@",@"小雪.png"];
        }
        if(NSNotFound != [[MapAndLocation shareLocation].weather rangeOfString:@"小雨"].location)
        {
            fileName = [[NSString alloc]initWithFormat:@"%@",@"中雨.png"];
        }
        if(NSNotFound != [[MapAndLocation shareLocation].weather rangeOfString:@"雨加雪"].location)
        {
            fileName =[[NSString alloc]initWithFormat:@"%@", @"雨夹雪.png"];
        }
        if(NSNotFound != [[MapAndLocation shareLocation].weather rangeOfString:@"阵雨"].location)
        {
            fileName =[[NSString alloc]initWithFormat:@"%@", @"中雨.png"];
        }
        if(NSNotFound != [[MapAndLocation shareLocation].weather rangeOfString:@"雷阵雨"].location)
        {
            fileName =[[NSString alloc]initWithFormat:@"%@", @"雷阵雨.png"];
        }
        if(NSNotFound != [[MapAndLocation shareLocation].weather rangeOfString:@"大雨转晴"].location)
        {
            fileName =[[NSString alloc]initWithFormat:@"%@", @"大雨转晴.png"];
        }
        if(NSNotFound != [[MapAndLocation shareLocation].weather rangeOfString:@"阴转晴"].location)
        {
            fileName =[[NSString alloc]initWithFormat:@"%@", @"阴转晴.png"];
        }
        
        if([MapAndLocation shareLocation].weather !=nil)
        {
            self.detailLabel.text = sweather;
        }
        //NSLog(fileName);
        self.weatherImage.image = [UIImage imageNamed:fileName];
        
    }];
    
}

- (void)loadView{

   [self nineIcon];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight - kP6Height(210)) collectionViewLayout:layout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.view = self.collectionView;
    self.navigationController.navigationBar.translucent =NO;
    self.navigationItem.title = @"综合";
    [self.collectionView registerClass:[HeadCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeadCollectionReusableView"];

    GroundGlass *backImage = [[GroundGlass alloc]initWithFrame:[UIScreen mainScreen].bounds style:UIBlurEffectStyleExtraLight];
    self.collectionView.backgroundView = backImage;

    self.collectionView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefresh)];
}
- (void)headerRefresh
{
    [self cycleScrollView];
    [self.collectionView.header endRefreshing];
    [self judgeForNetWork];

}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.compositeArray.count;
}

-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"item";
    
    [collectionView registerClass:[CompositCollectionViewCell class] forCellWithReuseIdentifier:cellIdentifier];
    
    CompositCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];
    cell.compositImageView.image = self.iconArray[indexPath.row];
    cell.compositLabel.text = self.compositeArray[indexPath.row];
    return cell;
}
// 跳转方法  每个case 是一个 界面 下标对应九宫格
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.row) {
        case  0:{
            SportViewController *sport = [[SportViewController alloc]init];
            sport.tag = 100;
            [self presentViewController:sport animated:NO completion:nil];
        }
            break;
        case 1:{
            SportViewController *sport = [[SportViewController alloc]init];
            sport.tag = 101;
            [self presentViewController:sport animated:NO completion:nil];
            
        }
            break;
        case 2:{
            CarBossViewController *boss = [[CarBossViewController alloc]init];
            [self presentViewController:boss animated:NO completion:nil];
            
        }
            break;
        case 3:{
            ScienceTechnologyViewController *stVC = [[ScienceTechnologyViewController alloc] init];
            [self.navigationController pushViewController:stVC animated:YES];
            
        }
            break;
        case 4:{
            DigitalChannelViewController *digitalChannelVc = [[DigitalChannelViewController alloc] init];
            [self.navigationController pushViewController:digitalChannelVc animated:YES];
        }
            break;
        case 5:{
            MovieTableViewController *movieVC = [[MovieTableViewController alloc]init];
            movieVC.tag = 200;
            [self.navigationController pushViewController:movieVC animated:YES];
        }
            break;
        case 6:{
            ManOutfitViewController *manOutfitVC = [[ManOutfitViewController alloc] init];
            manOutfitVC.navigationController.navigationBar.backgroundColor = [UIColor blackColor];
            [self.navigationController pushViewController:manOutfitVC animated:YES];
            
        }
            break;
        case 7:{
            MovieTableViewController *movieVC = [[MovieTableViewController alloc]init];
            movieVC.tag = 100;
            [self.navigationController pushViewController:movieVC animated:YES];
        }
            break;
        case 8:{
            MovieTableViewController *movieVC = [[MovieTableViewController alloc]init];
            movieVC.tag = 300;
            [self.navigationController pushViewController:movieVC animated:YES];
        }
            break;
        default:
            break;
    }
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
    {
    
    return CGSizeMake(kWidth / 3, kWidth / 3 - 10);
    
    }

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
    {
    
    
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
    {
    
    return 0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    
    return 0;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    
    return CGSizeMake(kWidth, kP6Height(210));
    
    
}
- (UICollectionReusableView*)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]){
        
        HeadCollectionReusableView *reusble = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeadCollectionReusableView" forIndexPath:indexPath];

        reusble.scrollView = [self cycleScrollView];
        
        return reusble;
    }
    else return nil;
}

#pragma mark --- 首页轮播图 ---

- (SDCycleScrollView*)cycleScrollView
{
    self.scrollArray = [NSMutableArray array];
    //self.scrollArray = [ACHCommon mainScrollViewAsynchronousURL:kScrollURL];
    [ACHNetWork GET:kScrollURL success:^(id response) {
        
        self.titleArray = [NSMutableArray array];
        self.imageArray = [NSMutableArray array];
        self.idArray = [NSMutableArray array];
        NSArray *arr = [response objectForKey:@"top_stories"];
        for (NSMutableDictionary *dic in arr) {
            NSString *titleStr = [dic objectForKey:@"title"];
            NSString *imageStr = [dic objectForKey:@"image"];
            NSString *model = [dic objectForKey:@"id"];
            [_titleArray addObject:titleStr];
            [_imageArray addObject:imageStr];
            [_idArray addObject:model];
        }
   
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
        SDCycleScrollView *cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, kWidth, kP6Height(210)) imageURLStringsGroup:_scrollArray];
    cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
    cycleScrollView.delegate = self;
    [self.view addSubview:cycleScrollView];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        cycleScrollView.titlesGroup = _titleArray;
        cycleScrollView.imageURLStringsGroup = _imageArray;
        
        cycleScrollView.contentMode = UIViewContentModeTop;
        cycleScrollView.clipsToBounds = YES;
        
        cycleScrollView.layer.shadowColor = [UIColor blackColor].CGColor;
        cycleScrollView.layer.shadowOpacity = 0.6;
        cycleScrollView.layer.shadowRadius = 1.0;
        cycleScrollView.clipsToBounds = NO;
    });
    return cycleScrollView;
}
//点击轮播图触发的方法
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    MovieDetailViewController *movieDetailVC = [[MovieDetailViewController alloc]init];
    NSString *str = _idArray[index];
    NSString *imaStr = _imageArray[index];
    NSString *titleStr = _titleArray[index];
    movieDetailVC.pinjieUrl = imaStr;
    movieDetailVC.idString = str;
    movieDetailVC.titleString = titleStr;
    movieDetailVC.temp = 1000;
    [self.navigationController pushViewController:movieDetailVC animated:NO];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)nineIcon{
    self.compositeArray = @[@"今日看点",@"体育",@"汽车",@"科技",@"数码",@"游戏世界",@"男人装",@"电影志",@"不许无聊",];
  
   self. iconArray = @[[UIImage imageNamed:@"news"],[UIImage imageNamed:@"sport"],[UIImage imageNamed:@"car"],[UIImage imageNamed:@"genius"],[UIImage imageNamed:@"shuma"],[UIImage imageNamed:@"youxi"],[UIImage imageNamed:@"nanren"],[UIImage imageNamed:@"dianying"],[UIImage imageNamed:@"buxu"]];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

    


@end

